<?php

namespace app\services\face;

use think\Exception;
use think\facade\App;

class ZipService
{
    protected $errorInfo = '' ;
    protected $zipPath = '' ;
    protected $zipName = '' ;
    protected $zipUrl = '' ;
    protected $uploadDir = '' ;
    protected function makeZip($pathArr) {
        $zip = new \ZipArchive();

        if($zip->open($this->zipName,\ZipArchive::CREATE|\ZipArchive::OVERWRITE)){
            foreach($pathArr as $file){
                if(!file_exists($file)){
                    continue;
                }
                //向压缩包中添加文件
                $zip->addFile($file,basename($file));
            }
            $zip->close();
            return true ;
        }else{
            $this->errorInfo = '生成失败' ;
            return false ;
        }
    }

    public function createData($pathArr ) {

        if($pathArr == []){
            $this->errorInfo = '数据不存在' ;
            return false ;
        }
        $data = [] ;


        $upload = 'upload/zip/' .  date('YmdHis') .rand(10000,99999);
        if(!is_dir($upload)){
            mkdir(iconv("UTF-8", "GBK", $upload ),0777,true);
        }
        $root = App::getRootPath() . 'public/' ;
        $this->zipPath = $root . $upload . '/';

        foreach ($pathArr as $k=>$val)
        {
            $val2 = $this->replace_domain($val['url']) ;
            copy($val2 , $upload  . '/' . $val['name'] . '.jpg' ) ;
            array_push($data , $this->zipPath  . $val['name'] . '.jpg' ) ;
        }
        $zip = time().'.zip' ;
        $this->zipName = $this->zipPath .$zip;
        $this->zipUrl = $upload . '/' .$zip;


        $this->makeZip($data);

        return true ;




    }
    public  function replace_domain($url)
    {
        $url =  str_replace(request()->domain() . '/' , '' , $url) ;
        return $url ;
    }

    public function downloadFile($name,$fileUrl){

        ini_set('memory_limit', '500M');  //also see memory_limit in php.ini
        set_time_limit(0);
        if(file_exists($fileUrl) == false)
        {
            throw new Exception('文件不存在');
        }

        $file = fopen($fileUrl,'r');

        $extArr = explode('.' , $fileUrl) ;
        $ext = end($extArr) ;
        header("Content-type: application/octet-stream");

        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-disposition: attachment; filename='.$name.'.' .$ext );

        header('Content-Length: ' . filesize($fileUrl));

        ob_clean();
        flush();
        //输出 读区到的文件内容 （读文件流）
        echo fread($file,filesize($fileUrl));

        //关闭服务器文件
        fclose($file);
//        $this->deleteDir($this->zipPath) ;

    }


    public function getError()
    {
        return $this->errorInfo ;
    }
   public function getPath()
    {
        return $this->zipName ;
    }
    public function getZipUrl()
    {
        return $this->zipUrl ;
    }
    public function deleteDir($dir)
    {
        if (rmdir($dir)==false && is_dir($dir)) {
            if ($dp = opendir($dir)) {
                while (($file=readdir($dp)) != false) {
                    if (is_dir($file) && $file!='.' && $file!='..') {
                        $this->deleteDir($file);
                    } else {
                        unlink($file);
                    }
                }
                closedir($dp);
            } else {
                exit('Not permission');
            }
        }
    }

}